﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using NHibernate;
using ZLCMS.Business;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.TypeList
{
    public partial class Detail : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);
            if (!IsPostBack)
            {
                ViewState["Kind"] = Request["Kind"];
                ViewState["uid"] = Request["uid"];
                ViewState["tabid"] = Request["tabid"];

                //初始化控件值
                InitializeContent();
            }

            if (IsPostBack)
            {
                Save();
            }
        }


        protected void InitializeContent()
        {
            string uid = ViewState["uid"] as string;
            if (!string.IsNullOrEmpty(uid))
            {
                using(ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    CmsTreeDef def = session.Get<CmsTreeDef>(Int32.Parse(uid));
                    tb_name.Text = def.Name;
                    tb_value.Text = def.Value;
                }
            }

        }

        protected void Save()
        {
            CmsTreeDef def = new CmsTreeDef();
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {

                string uid = ViewState["uid"] as string;
                if (!string.IsNullOrEmpty(uid))
                {
                    def = session.Get<CmsTreeDef>(Int32.Parse(uid));
                }
                def.Name = tb_name.Text;
                def.Value = tb_value.Text;
                def.Kind = ViewState["Kind"] as string;

                session.SaveOrUpdate(def);
                session.Flush();

                ParamKit.GetInstance().Fresh("TreeDefType");
            }

            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = ViewState["tabid"] as string
            });
        }
    }
}
